<template>
  <div class="flex flex-col h-full">
    <SearchContainer @search="getData" @reset="() => formRef.resetFields()">
      <a-form ref="formRef" class="form-style-flex" :model="formState">
        <a-form-item name="zjm" class="w-[310px]">
          <a-input v-model:value="formState.zjm" placeholder="药品编号/药品名称/助记码" />
        </a-form-item>
        <a-form-item name="month" label="核算月度" class="w-[310px] ml-3">
          <a-select v-model:value="formState.month" placeholder="请选择" :options="options" :field-names="{ label: 'month', value: 'month' }" allow-clear />
        </a-form-item>
        <a-form-item name="showZeroProject" label="显示0元项目" class="ml-3">
          <a-switch v-model:checked="formState.showZeroProject" @change="getData" />
        </a-form-item>
      </a-form>
    </SearchContainer>
    <a-card class="mt-2 flex-1">
      <a-space class="flex justify-end">
        <BaseExport type="药库" name="收发存查询" code="00001012" :search-params="formState">导出</BaseExport>
      </a-space>
      <BaseTable :loading="loading" column-code="00001012" :data-source="tableData" :pagination="pagination" bordered fill>
        <template #bodyCell="{ column, index, record }">
          <template v-if="column.dataIndex === 'xh'">
            <div>{{ index + 1 }}</div>
          </template>
          <template v-if="column.dataIndex === 'mc'">
            <MedicineInfo :value="{ mc: record.mc, gg: record.gg, cj: record.cj }" />
          </template>
          <template v-if="column.dataIndex === 'action'">
            <a-button type="link" size="small" @click="onHandleEdit(record)"> 收发存明细 </a-button>
            <a-button type="link" size="small" @click="onPrint(record)"> 打印 </a-button>
          </template>
        </template>
        <template #summary>
          <a-table-summary fixed>
            <a-table-summary-row>
              <a-table-summary-cell>合计</a-table-summary-cell>
              <a-table-summary-cell>
                <a-typography-text>-</a-typography-text>
              </a-table-summary-cell>
              <a-table-summary-cell>
                <a-typography-text>-</a-typography-text>
              </a-table-summary-cell>
              <a-table-summary-cell>
                <a-typography-text>-</a-typography-text>
              </a-table-summary-cell>
              <a-table-summary-cell>
                <a-typography-text>-</a-typography-text>
              </a-table-summary-cell>
              <a-table-summary-cell>
                <a-typography-text>-</a-typography-text>
              </a-table-summary-cell>
              <a-table-summary-cell>
                <a-typography-text>-</a-typography-text>
              </a-table-summary-cell>
              <a-table-summary-cell>
                <a-typography-text>
                  <div class="text-right">{{ formatAmount(total?.jeQc) }}</div>
                </a-typography-text>
              </a-table-summary-cell>
              <a-table-summary-cell>
                <a-typography-text>-</a-typography-text>
              </a-table-summary-cell>
              <a-table-summary-cell>
                <a-typography-text>-</a-typography-text>
              </a-table-summary-cell>
              <a-table-summary-cell>
                <a-typography-text>
                  <div class="text-right">{{ formatAmount(total?.jeSr) }}</div>
                </a-typography-text>
              </a-table-summary-cell>
              <a-table-summary-cell>
                <a-typography-text>-</a-typography-text>
              </a-table-summary-cell>
              <a-table-summary-cell>
                <a-typography-text>
                  <div class="text-right">{{ formatAmount(total?.jeFc) }}</div>
                </a-typography-text>
              </a-table-summary-cell>
              <a-table-summary-cell>
                <a-typography-text>-</a-typography-text>
              </a-table-summary-cell>
              <a-table-summary-cell>
                <a-typography-text>
                  <div class="text-right">{{ formatAmount(total?.jeQm) }}</div>
                </a-typography-text>
              </a-table-summary-cell>
            </a-table-summary-row>
          </a-table-summary>
        </template>
      </BaseTable>
    </a-card>
    <Edit v-model:open="editOpen" :row-data="rowData" />
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { message } from 'ant-design-vue'
import { post } from '@/utils/request'
import SearchContainer from '@/components/SearchContainer'
import BaseTable from '@/components/BaseTable'
import { formatAmount } from '@/utils/format.js'
import Edit from './Edit'

const rowData = ref(null)
const editOpen = ref(false)
const options = ref([])
const formRef = ref(null)
const formState = ref({
  showZeroProject: false
})
const total = ref({})
const getCalculateData = async () => {
  const { code, data, msg } = await post('/drugStorage/comprehenive/getMonthWeightedAverageList', {})
  if (code !== 0) return message.error(msg)
  options.value = data
}

const { loading, tableData, getData, pagination } = useTable({
  url: '/drugStorage/comprehenive/getDrugMonthBillList',
  immediate: true,
  params: formState.value,
  formatResult: (result) => {
    total.value = result.reduce(
      (sum, current) => {
        return {
          jeQc: sum.jeQc + (current.jeQc ? Number(current.jeQc) : 0),
          jeSr: sum.jeSr + (current.jeSr ? Number(current.jeSr) : 0),
          jeFc: sum.jeFc + (current.jeFc ? Number(current.jeFc) : 0),
          jeQm: sum.jeQm + (current.jeQm ? Number(current.jeQm) : 0)
        }
      },
      { jeQc: 0, jeSr: 0, jeFc: 0, jeQm: 0 }
    )
    return result
  }
})

const onHandleEdit = (row) => {
  rowData.value = row
  editOpen.value = true
}

// 打印
const onPrint = async (record) => {
  const { code, msg, data } = await post('/drugStorage/comprehenive/printDrugMonthBill', { month: record.month })
  if (code !== 0) return message.error(msg || '获取打印数据失败')
  window.openExe?.rpt(data) //调用winform打印
}

onMounted(() => {
  getCalculateData()
})
</script>
<style lang="less" scoped>
.ant-picker {
  width: 100%;
}
</style>
